<!-- 运费模板添加&修改 -->
<template>
  <div>
    <el-dialog
      :title="showTitle"
      :visible.sync="showDialog"
      width="600px"
      @close="btnCancel"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="formData"
        label-width="80px"
        style="width: 100%"
      >
        <el-form-item
          label="模板类型"
          prop="templateType"
        >
          <el-select
            v-model="formData.templateType"
            clearable
            placeholder="请选择模板类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in templateTypeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item
          label="运送类型"
          prop="transportType"
        >
          <el-select
            v-model="formData.transportType"
            clearable
            placeholder="请选择运送类型"
            style="width: 100%"
          >
            <el-option
              v-for="item in transportTypeOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>

        </el-form-item>
        <el-form-item
          v-show="formData.templateType === 3"
          label="关联城市"
          prop="associatedCityList"
        >
          <el-checkbox-group
            v-model="formData.associatedCityList"
          >
            <el-checkbox label="2">京津冀</el-checkbox>
            <el-checkbox label="3">江浙沪</el-checkbox>
            <el-checkbox label="4">川渝</el-checkbox>
            <el-checkbox label="5">黑吉辽</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item
          v-show="formData.templateType !== 3"
          label="关联城市"
        >
          <span>全国</span>
        </el-form-item>
        <el-form-item
          label="首重价格"
          prop="firstWeight"
        >
          <el-input
            v-model="formData.firstWeight"
            placeholder="请输入首重价格"
          >
            <span
              slot="suffix"
              style="
                    color: #20232a;
                    font-weight: 400;
                    font-size: 14px;
                    font-family: PingFangSC, PingFangSC-Regular;
                    margin-right: 14px;
                  "
            >元</span>
          </el-input>
        </el-form-item>
        <el-form-item
          label="续重价格"
          prop="continuousWeight"
        >
          <el-input
            v-model="formData.continuousWeight"
            placeholder="请输入续重价格"
          >
            <span
              slot="suffix"
              style="
                    color: #20232a;
                    font-weight: 400;
                    font-size: 14px;
                    font-family: PingFangSC, PingFangSC-Regular;
                    margin-right: 14px;
                  "
            >元</span>
          </el-input>
        </el-form-item>
        <el-form-item
          label="轻抛系数"
          prop="lightThrowingCoefficient"
        >
          <el-input
            v-model="formData.lightThrowingCoefficient"
            placeholder="请输入轻抛系数"
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center"
      >
        <el-button
          class="save-btn"
          @click="btnOK"
        >确认</el-button>
        <el-button
          class="cancel-btn"
          @click="btnCancel"
        >取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { freightManageOperate } from '@/api/transit'
export default {
  data() {
    return {
      showDialog: false,
      formData: {
        associatedCityList: [],
        continuousWeight: null,
        firstWeight: null,
        lightThrowingCoefficient: null,
        templateType: null,
        transportType: null
      },
      templateTypeOptions: [
        {
          id: 1,
          name: '同城寄'
        },
        {
          id: 2,
          name: '省内寄'
        },
        {
          id: 3,
          name: '经济区互寄'
        },
        {
          id: 4,
          name: '跨省'
        }
      ],
      transportTypeOptions: [
        {
          id: 1,
          name: '普快'
        },
        {
          id: 2,
          name: '特快'
        }
      ],
      rules: {
        templateType: [
          { required: true, message: '模板类型不能为空', trigger: 'change' }
        ],
        transportType: [
          { required: true, message: '运送类型不能为空', trigger: 'change' }
        ],
        associatedCityList: [
          { required: true, message: '关联城市不能为空', trigger: 'change' }
        ],
        firstWeight: [
          { required: true, message: '首重价格不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!/^([1-9]\d{0,2}|999.9)(\.\d{1})?$/.test(value)) {
              callback(new Error('请输入1-999间保留小数点后一位的数值'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        continuousWeight: [
          { required: true, message: '续重价格不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!/^([1-9]\d{0,2}|999.9)(\.\d{1})?$/.test(value)) {
              callback(new Error('请输入1-999间保留小数点后一位的数值'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        lightThrowingCoefficient: [
          { required: true, message: '轻抛系数不能为空', trigger: 'blur' },
          { validator: (rule, value, callback) => {
            if (!/^[1-9]{1}\d{0,4}?$/.test(value)) {
              callback(new Error('请输入1-99999间的整数'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '编辑模板' : '新增模板'
    }
  },
  methods: {
    // 打开弹层
    openDialog() {
      this.showDialog = true
    },
    // 取消
    btnCancel() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.formData = {
        id: null,
        templateType: '',
        transportType: '',
        associatedCityList: [],
        firstWeight: '',
        continuousWeight: '',
        lightThrowingCoefficient: ''
      }
    },
    // 确认
    btnOK() {
      if (this.formData.templateType !== 3) {
        this.formData.associatedCityList = ['1']
      }
      this.$refs.form.validate(async(isOK) => {
        if (isOK) {
          if (this.formData.id) {
            const res = await freightManageOperate(this.formData)
            if (res.code === 200) {
              this.$message.success('更新模板成功')
              this.$emit('updateFreightTemplateList')
            } else {
              this.$message.error(res.msg)
            }
          } else {
            const res = await freightManageOperate(this.formData)
            if (res.code === 200) {
              this.$message.success('新增模板成功')
              this.$emit('updateFreightTemplateList')
            } else {
              this.$message.error(res.msg)
            }
          }
          this.btnCancel()
        }
      })
    },
    getFreightTemplateDetail(row) {
      Object.assign(this.formData, row)
    }
  }
}
</script>
<style lang="scss" scoped>
.add-form-dialog {
  ::v-deep .el-dialog__body {
    padding: 40px 60px 0 50px !important;
  }
}
</style>
